<?php namespace system\model;

use hdphp\Model\Model;

class HomeUser extends Model{

	//数据表
	protected $table = "home_user";

	//完整表名
	protected $full = false;

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
		['uname','required','请输入电话号',3,3],
		['uname','phone','请输入正确格式的电话',3,3],
		['uname','unique','用户已存在',3,3],
		['upassword','required','请输入密码',3,3],
		['code','required','请输入验证码',3,3],
		['upassword','confirm:aginPassword','两次输入密码不一致',3,3],


	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
	];

	//自动过滤
    protected $filter=[
        //[表单字段名,过滤条件,处理时间]
    ];

	//时间操作,需要表中存在created_at,updated_at字段
	protected $timestamps=false;


	//禁止插入的字段
	protected $denyInsertFields = [];

	//禁止更新的字段
	protected $denyUpdateFields = [];

	//前置方法 比如: _before_add 为添加前执行的方法
	protected function _before_add(){}
	protected function _before_delete(){}
	protected function _before_save(&$data){}

	protected function _after_add(){}
	protected function _after_delete(){}
	protected function _after_save(){}
//	 注册
	public function register(){
		if(!$this->create()) return false;
		//验证验证码是否正确
		if(strtoupper($_POST['code'])!=strtoupper($_SESSION['code'])){
			p($_POST['code']);p($_SESSION['code']);die;
			$this->error='验证码错误';return false;
		}
		//p($_POST['upassword']);die;
		$this->data['upassword']=md5($_POST['upassword']);
		$this->data['aginPassword']=md5($_POST['aginPassword']);


		//写入数据库
//		p($_POST);
//		p($this->data);die;
		$data=$this->add();

		if(!$data){
			$this->error=$this->getError();return false;
		}
		return true;

	}
	//登录
	public  function login(){
		//执行验证
		$this->validate=[
			['uname','required','请输入账号',3,3],
			['upassword','required','请输入密码',3,3],
			['code','required','请输入验证码',3,3],
		];
		//执行create
		if(!$this->create()) return false;
//		检验验证码是否正确
		if(strtoupper($_POST['code'])!=strtoupper($_SESSION['code'])){
			$this->error='验证码错误';return false;
		}
		//验证用户名是否正确 原理拿过来的用户名去数据库中取得一条数据看能否取到
		if(!$data=$this->where('uname',$_POST['uname'])->first()){
			$this->error='用户名不存在';return false;
		}
		//密码是否正确

		if(md5($_POST['upassword'])!=$data['upassword']){
			//echo 1;exit;
			$this->error='密码错误';return false;
		}
		$_SESSION['homeUser']['uid']=$data['uid'];
		$_SESSION['homeUser']['uname']=$data['uname'];
		return true;
	}
		//密码重置
public function changePass(){
	$this->validate=[
		['upassword','required','请输入密码',3,3],
		['code','required','请输入验证码',3,3],
		['upassword','confirm:aginPassword','两次输入密码不一致',3,3],
	];
		if(!$this->create()) return false;
		//验证验证码是否正确
		if(strtoupper($_POST['code'])!=strtoupper($_SESSION['code'])){
			p($_POST['code']);p($_SESSION['code']);die;
			$this->error='验证码错误';return false;
		}
	//验证原有用户名是否正确 原理拿过来的用户名去数据库中取得一条数据看能否取到

	if(!$data=$this->where('uname',$_POST['uname'])->first()){

		$this->error='这个用户没有注册';return false;
	}
		//p($_POST['upassword']);die;
		$this->data['upassword']=md5($_POST['upassword']);
		$this->data['aginPassword']=md5($_POST['aginPassword']);


		//写入数据库
	//		p($_POST);
	//		p($this->data);die;
$_POST['upassword']=md5($_POST['upassword']);


		$this->where('uname',$_POST['uname'])->update($_POST);

		if(!$data){
			$this->error=$this->getError();return false;
		}
		return true;

		}
}